package com.mjk.common.core.jdbc.ibatis.typehandler.handler;

import com.mjk.common.base.model.SystemDate;
import com.mjk.common.core.jdbc.ibatis.typehandler.BaseHandler;
import com.mjk.common.core.jdbc.service.plus.provider.BeeDataSourceType;
import com.mjk.common.core.jdbc.util.TableUtils;
import com.mjk.common.util.ObjectUtils;
import com.mjk.common.util.TimeUtils;

import java.lang.reflect.Field;
import java.sql.Timestamp;
import java.util.Date;

public class TimestampHandler implements BaseHandler<Timestamp> {

	@Override
	public String resovleToString(Field f,Timestamp t) {
		if(ObjectUtils.isEmpty(t)){
            return null;
		}
		if(t instanceof SystemDate){
			return "sysdate";
		}
		String driverName = TableUtils.getDriverName();
		if(driverName.contains(BeeDataSourceType.ORACLE_WRAP.getDriver()) || driverName.contains(BeeDataSourceType.DM_WRAP.getDriver())){
			if(t.getHours()==0 && t.getMinutes()==0 && t.getSeconds()==0){
				return "to_date('" + TimeUtils.formatTime(new Date(t.getTime()), TimeUtils.DATE_FORMAT_DATEONLY) + "','yyyy-mm-dd')";
			}else{
				return "to_date('" + TimeUtils.formatTime(new Date(t.getTime()), TimeUtils.DATE_FORMAT_DATETIME) + "','yyyy-mm-dd hh24:mi:ss')";
			}
			
		}
		return TimeUtils.formatTime(new Date(t.getTime()), TimeUtils.DATE_FORMAT_DATETIME);
	}

	@Override
	public Timestamp resovleResult(Object o) {		
		Date date = TimeUtils.parse(o.toString());
		return  new Timestamp(date.getTime());
	}

}
